﻿#include <iostream>
#include "human.h"
#include <graphics.h>
#include <conio.h>
using namespace std;
IMAGE manL;
IMAGE manR;
IMAGE grass;
IMAGE water;
IMAGE wood;
IMAGE maninwoodL;
IMAGE maninwoodR;
IMAGE arrowL;
IMAGE arrowR;
IMAGE arrowLinwater;
IMAGE arrowRinwater;
IMAGE arrowLinwood;
IMAGE arrowRinwood;
IMAGE archerL;
IMAGE archerR;
IMAGE archerinwoodL;
IMAGE archerinwoodR;
int main()
{
	inigame();

	while (1) {
		mandraw();
		manmove();
		way();
		if (archer.moveCD == 2) {
			archermoveJudge();
			archerway();
			archer.moveCD = 0;
		}
		Sleep(100);
		CDcount();
	}
	getchar();
}
void inigame() {
	initgraph(1500,800);
	loadimage(&manL, _T("贴图//manL.jpg"),50,50);
	loadimage(&manR, _T("贴图//manR.jpg"), 50, 50);
	loadimage(&grass, _T("贴图//grass.jpg"), 50, 50);
	loadimage(&water, _T("贴图//water.jpg"), 50, 50);
	loadimage(&wood, _T("贴图//wood.jpg"), 50, 50);
	loadimage(&maninwoodR, _T("贴图//maninwoodR.jpg"), 50, 50);
	loadimage(&maninwoodL, _T("贴图//maninwoodL.jpg"), 50, 50);
	loadimage(&arrowL, _T("贴图//arrowL.jpg"), 50, 50);
	loadimage(&arrowLinwood, _T("贴图//arrowLinwood.jpg"), 50, 50);
	loadimage(&arrowLinwater, _T("贴图//arrowLinwater.jpg"), 50, 50);
	loadimage(&arrowRinwood, _T("贴图//arrowRinwood.jpg"), 50, 50);
	loadimage(&arrowRinwater, _T("贴图//arrowRinwater.jpg"), 50, 50);
	loadimage(&arrowR, _T("贴图//arrowR.jpg"), 50, 50);
	loadimage(&archerinwoodR, _T("贴图//archerinwoodR.jpg"), 50, 50);
	loadimage(&archerinwoodL, _T("贴图//archerinwoodL.jpg"), 50, 50);
	loadimage(&archerL, _T("贴图//archerL.jpg"), 50, 50);
	loadimage(&archerR, _T("贴图//archerR.jpg"), 50, 50);
	human.xy[0].x = 0;
	human.xy[0].y= 0;
	human.speed = 50;
}
void mandraw() {
	int i, j;
	for (i = 0; i < 16; i++) {
		for (j = 0; j < 30; j++) {
			switch (map[i][j]) {
			case 1:
				if (human.direction == 0) {
					switch (memoryblock) {
					case 0:
						putimage(50 * j, 50 * i, &manL);
						break;
					case 3:
							putimage(50 * j, 50 * i, &maninwoodL);
							break;
					}
				}
				else {
					switch (memoryblock) {
					case 0:
						putimage(50 * j, 50 * i, &manR);
						break;
					case 3:
						putimage(50 * j, 50 * i, &maninwoodR);
						break;
					}
				}
				human.xy[0].x = 50*j;
				human.xy[0].y = 50*i;
				break;
			case 0:
				putimage(50 * j, 50 * i, &grass);
				break;
			case 2:
				putimage(50 * j, 50 * i, &water);
				break;
			case 3:
				putimage(50 * j, 50 * i, &wood);
				break;
			case 4:
				arrowLPut(memoryblock2, i, j);
				break;
			case 5:
					arrowRPut(memoryblock2, i, j);
				break;
			case 6:
				if (archer.direction == 0) {
					switch (memoryblock4)
					{
					case 0:
						putimage(50 * j, 50 * i, &archerL);
						break;
					case 3:
						putimage(50 * j, 50 * i, &archerinwoodL);
						break;
					default:
						break;
					}

				}
				else {
					switch (memoryblock4) {
					case 0:
						putimage(50 * j, 50 * i, &archerR);
						break;
					case 3:
						putimage(50 * j, 50 * i, &archerinwoodR);
						break;
					default:
						break;
					}
				}
				break;
			case 7:
				arrowLPut(memoryblock3, i, j);
				break;
			case 8:
				arrowRPut(memoryblock3, i, j);
				break;
			}
		}
	}
}
void way() {
	switch (human.way) {
	case LEFT:
		if (y != 0&&map[x][y-1]!=2 && map[x][y - 1] != 6 && map[x][y - 1] != 5 && map[x][y - 1] != 4 && map[x][y - 1] != 7 && map[x][y - 1] != 8) {
			map[x][y] = memoryblock;
			y--;
			memoryblock = map[x][y];
			map[x][y] = 1;
		}
		human.direction = 0;
		break;
	case RIGHT:
		if (y != 29&& map[x][y + 1] !=2 && map[x][y + 1] != 6 && map[x][y + 1] != 4 && map[x][y + 1] != 5 && map[x][y + 1] != 7 && map[x][y + 1] != 8) {
			map[x][y] = memoryblock;
			y++;
			memoryblock = map[x][y];
			map[x][y] = 1;
		}
		human.direction = 1;
		break;
	case UP:
		if (x != 0&& map[x-1][y ]!=2  && map[x-1][y ] != 6 && map[x - 1][y] != 4 && map[x - 1][y] != 5 && map[x - 1][y] != 7 && map[x - 1][y] != 8) {
			map[x][y] = memoryblock;
			x--;
			memoryblock = map[x][y];
			map[x][y] = 1;
		}
		break;
	case DOWN:
		if (x != 15&& map[x + 1][y] != 2 && map[x + 1][y] != 6 && map[x + 1][y] != 4 && map[x + 1][y] != 5 && map[x + 1][y] != 7 && map[x + 1][y] != 8) {
			map[x][y] = memoryblock;
			x++;
			memoryblock = map[x][y];
			map[x][y] = 1;
		}
		break;
	case Shoot:
		if (human.arrowCD ==15) {
			human.arrowCD = 0;
				arrowshoot(human.direction,x,y,memoryblock2,human.arrowtype);
			}
		break;
	}
}
void manmove() {
	if (!_kbhit()) {
		human.way = STAY;

	}
	else {
		char ch;
		ch = _getch();
		switch (ch) {
		case 'A':
		case 'a':
			human.way = LEFT;
			break;
		case 'w':
		case 'W':
			human.way = UP;
			break;
		case 'D':
		case 'd':
			human.way = RIGHT;
			break;
		case 'S':
		case 's':
			human.way = DOWN;
			break;
		case 'j':
		case 'J':
			human.way = Shoot;
			break;
		}
	}
	
}
void refresh() {
	mandraw();
	manmove();
	way();
	if (archer.moveCD == 2) {
		archermoveJudge();
		archerway();
		archer.moveCD = 0;
	}
	CDcount();
	Sleep(200);
}
void arrowshoot(bool direction,int x,int y,int& memoryblock,int type) {
	int i;
	int shootx = x;
	int shooty = y;
	switch (direction) {
	case 0:
		for (i = 1; i <= arrow.range; i++) {
			if (y - i < 0||map[x][y-i]==1|| map[x][y - i] == 6 || map[x][y - i] == 4 || map[x][y - i] == 5 || map[x][y - i] == 7 || map[x][y - i] == 8) {
				break;
			}
          memoryblock=map[shootx][shooty - i]  ;
		  if (type == 1) {
			  map[shootx][shooty - i] = 4;
		  }
		  else
			  map[shootx][shooty - i] = 7;
			refresh();
			map[shootx][shooty - i] = memoryblock;

		}
		break;
	case 1:
		for (i = 1; i <= arrow.range; i++) {
			if (shooty + i >30 || map[x][y + i] == 1 || map[x][y + i] == 6 || map[x][y + i] == 4 || map[x][y + i] == 5|| map[x][y + i] == 7 || map[x][y + i] == 8) {
				break;
			}
			memoryblock = map[shootx][shooty + i];
			if (type == 1) {
				map[shootx][shooty + i] = 5;
			}
			else
				map[shootx][shooty + i] = 8;
			refresh();
			map[shootx][shooty + i] = memoryblock;
		}
		break;
	}
}
void archerway() {
	switch (archer.way) {
	case LEFT:
		if (archer.y != 0 && map[archer.x][archer.y - 1] != 2 && map[archer.x][archer.y - 1] != 1 && map[archer.x][archer.y - 1] != 4 && map[archer.x][archer.y - 1] != 5 && map[archer.x][archer.y - 1] != 7 && map[archer.x][archer.y - 1] != 8) {
			map[archer.x][archer.y] = memoryblock4;
			archer.y--;
			memoryblock4 = map[archer.x][archer.y];
			map[archer.x][archer.y] = 6;	
		}
		archer.direction = 0;
		break;
	case RIGHT:
		if (archer.y != 29 && map[archer.x][archer.y + 1] != 2 && map[archer.x][archer.y + 1] != 1 && map[archer.x][archer.y + 1] != 4 && map[archer.x][archer.y + 1] != 5 && map[archer.x][archer.y + 1] != 7 && map[archer.x][archer.y + 1] != 8) {
			map[archer.x][archer.y] = memoryblock4;
			archer.y++;
			memoryblock = map[archer.x][archer.y];
			map[archer.x][archer.y] = 6;
		}
		archer.direction = 1;
		break;
	case UP:
		if (archer.x != 0 && map[archer.x - 1][archer.y] != 2 && map[archer.x-1][archer.y ] != 1 && map[archer.x - 1][archer.y] != 4 && map[archer.x - 1][archer.y] != 5) {
			map[archer.x][archer.y] = memoryblock4;
			archer.x--;
			memoryblock4 = map[archer.x][archer.y];
			map[archer.x][archer.y] = 6;
		}
		break;
	case DOWN:
		if (archer.x != 15 && map[archer.x + 1][archer.y] != 2 && map[archer.x+1][archer.y ] != 1 && map[archer.x + 1][archer.y] != 4 && map[archer.x + 1][archer.y] != 5) {
			map[archer.x][archer.y] = memoryblock4;
			archer.x++;
			memoryblock4 = map[archer.x][archer.y];
			map[archer.x][archer.y] = 6;
		}
		break;
	case Shoot:
		if (archer.arrowCD == 15) {
			archer.arrowCD = 0;
			arrowshoot(archer.direction,archer.x,archer.y,memoryblock3,archer.arrowtype);
		}
		break;
	}
}
void CDcount(){
	if (human.arrowCD < 15) {
		human.arrowCD++;
	}
	if (archer.arrowCD < 15) {
		archer.arrowCD++;
	}
	if (archer.moveCD < 2) {
		archer.moveCD++;
	}
}
void archermoveJudge() {
	int deltax = archer.x - x;
	int deltay = archer.y - y;
	if (abs(deltay) > abs(deltax)) {
		if (deltay > 0&&deltay<=15) {
			if (map[archer.x][archer.y - 1] != 1)
				archer.way = LEFT;
			else archer.way = STAY;
		}
		else if (deltay < 0 && deltay >= -15) {
			if (map[archer.x][archer.y + 1] != 1)
				archer.way = RIGHT;
			else archer.way = STAY;
		}
		else archer.way=STAY;

	}
	else if (abs(deltay) <= abs(deltax)) {
		if (deltax > 0 && deltax <=15) {
			if (map[archer.x - 1][archer.y] != 1)
				archer.way = UP;
			else archer.way = STAY;
		}
		else if (deltax < 0 && deltax >=- 15) {
			if (map[archer.x + 1][archer.y] != 1)
				archer.way = DOWN;
			else archer.way = STAY;
		}
		else archer.way = STAY;
	}

	if (deltay > 0 && deltay <= 5&&deltax==0)
	{
		archer.direction = 0;
		archer.way = Shoot;
	}
	else if (deltay < 0 && deltay >= -5&&deltax == 0)
	{
		archer.direction = 1;
		archer.way = Shoot;
	}
	}

void arrowRPut(int memoryblock,int i,int j) {
	switch (memoryblock) {
	case 0:
		putimage(50 * j, 50 * i, &arrowR);
		break;
	case 2:
		putimage(50 * j, 50 * i, &arrowRinwater);
		break;
	case 3:
		putimage(50 * j, 50 * i, &arrowRinwood);
		break;
	}
}
void arrowLPut(int memoryblock, int i, int j) {
	switch (memoryblock) {
	case 0:
		putimage(50 * j, 50 * i, &arrowL);
		break;
	case 2:
		putimage(50 * j, 50 * i, &arrowLinwater);
		break;
	case 3:
		putimage(50 * j, 50 * i, &arrowLinwood);
		break;
	}
}
